<?php

class Menu_manage  extends AdminManage
{
	/**
	 * 初始化
	 */
	public function _init()
	{

	}

	/**
	 * 默认动作
	 */
	protected function _default()
	{
        $sql = "select `id`,menuId,menuName,isShow,action from menu where 1=1 order by menuId ASC";
    	$res = DB::select($sql);
        foreach ($res as $k=>$v){
            if($v['MENUID']<1000){
                $topMenu[] = $v;
            }else{
                $subMenu[] = $v;
            }
        }
        $res = $topMenu;
       foreach ($res as $k=>$v){
            foreach ($subMenu as $key=>$val){
                if($v['MENUID']== substr($val['MENUID'],0,3)){
                    $res[$k]['SUB'][] = $val;
                }
            }
       }
        $this->assign('topMenu',$topMenu);
        $this->assign('mysqlData',$res);      
		$this->tpl_full_path('/admins/manage/menu');
	}
	
	/**
     *ajax删除菜单
     */
     protected function _delMenu(){
        $id = $this->getParam('id','i');
        if(empty($id)){
            die("0");
        }else{
            if($id<1000){  //顶级
                $where = "left(menuId,3) = {$id}";
            }else{
                $where = "menuId = {$id}";
            }
            $sql = "delete from menu where ".$where ;
            if(DB::execute($sql)){
                die("1");
            }else{
                die("0");
            }
        }
    
    }
     /**
     *ajax是否显示菜单
     */
      protected function _changeMenuIsshow(){
        $id = $this->getParam('id','i');
        $type = $this->getParam('type','i');
        if(empty($id)){
            die('0');//参数错误
        }else{
            $sql = "update menu set isShow = {$type} where menuId = {$id}";
            if(DB::execute($sql)){
                die('1');
            }else{
                die('0');
            }
        }
      }
    
     /**
     *ajax更换动作
     */
    protected function _changeMenuAction(){
        $action  = $this->getParam('action','t');
        $id = $this->getParam('id','i');
        if(empty($action) || empty($id)){
            die('0'); //参数错误
        }else{
            $ids = substr($id,0,3);
            $sql = "select 1 from menu where left(menuId,3)={$ids} and action = '{$action}' "; 
            if(DB::select($sql)){ //已经存在
                die('2'); 
            }else{
                $sql = "update menu set action = '{$action}' where menuId = {$id}";
                if(DB::execute($sql)){
                    die('1'); //成功
                }else{
                    die('0'); //失败
                }
            }
        }
         
    }
    
    
    /**
     *ajax更换菜单名
     */
    protected function _changeMenuName(){
		//header("Content-Type:text/html;charset=utf8");
		$name = $_POST['name'];
		$name = urldecode($name);
		iconv("UTF-8", "GB2312", $name);
        //$name = Encode::decodeURI($name);
		//iconv("GB2312", "UTF-8//IGNORE", $name);

        $id = $this->getParam('id','i');
        if(empty($name) || empty($id)){
            die('0'); //参数错误
        }else{
            if($id<1000){
                $where = "menuId < 1000";
            }else{
                $ids = substr($id,0,3);
                $where = "left(menuId,3) = $ids ";
            }
            $sql = "select 1 from menu where ".$where." and menuName = '{$name}' ";
            if(DB::select($sql)){ //已经存在
                die('2'); 
            }else{
                $sql = "update menu set menuName = '{$name}' where menuId = {$id}";
                if(DB::execute($sql)){
                    die('1'); //成功
                }else{
                    die('0'); //失败
                }
            }
        }
        
    }
    
    
    /**
     *检查菜单名是否存在 
     */
    protected function _addMenu(){
        //$name = Encode::decodeURI($_REQUEST['name']);
		
		$name = $_POST['name'];
		$name = urldecode($name);
        $name = isset($name) && !empty($name) ? trim($name) : '';
		iconv("UTF-8", "GB2312", $name);
        $id = $this->getParam('id','i');
        $action = isset($_POST['action']) && !empty($_POST['action']) ?trim($_POST['action']) : '';
        if(empty($name)){
            die('fail');
        }else{ 
            if($id==0){
                $sql = "select menuId,menuName,action from menu where menuId<1000";
            }else{
                $sql = "select menuId,menuName,action from menu where left(menuId,3)={$id} and menuId>{$id}";
            }
            $res = DB::select($sql);
            if(count($res)>0){
                foreach ($res as $v){
                    $ids[] = $v['MENUID'];
                    $names[] = $v['MENUNAME'];
                    $actions[] = $v['ACTION'];
                }
                if(in_array($name,$names)){
                    die('namefail');
                }
                if($id>0){
                    if(in_array($action,$actions)){
                        die('actionfail');
                    }
                }
                $insertDB['menuId'] = max($ids)+1;
            }else{
                $insertDB['menuId'] = $id.'001';
            }
            $insertDB['action'] = $action;
            $insertDB['menuName'] = $name;
            $insertDB['isShow'] = '1';
            if(DB::insert('menu',$insertDB)){
                die('success'); //添加成功
            }else{
                die('fail'); //添加失败
            }
        }
    }
    
    
     /**
     * 删除记录
     */
    protected function _del() {
    	if ($_SERVER["REQUEST_METHOD"] == "POST"){
    		$choice = implode(',', $_REQUEST['choice']);
    	}else{
    		$choice =  addslashes($_REQUEST['id']);
    	}
        $whereClause = "id in ( ".$choice." ) and group_id <>5";	
    	$salesdatelRows = DB::select('select count(1) CNT from sales_auto_assign where sales_id in ('.$choice.')', -1);//业务分配系统总记录数
    	$shopapplylRows = DB::select('select count(1) CNT from company_shop_apply where salesid in ('.$choice.')', -1);//营销宝试用总记录数
    	if($salesdatelRows > 0 || $shopapplylRows>0){
    		$this->alertBack('当前数据存在客户分配记录或试用申请记录，请联系此业务员的相关主管，调整数据后才能删除此用户！');
    		return false;
    	}
        $_REQUEST['state'] = "0"; 
    	if(DB::update('system',$whereClause)){   //func_del('system',$whereClause)
            $c['status']=0;
            DB::update("corp_sell_protect","salesid in(".$choice.")",$c);//把保护的设为0(已离职)
    		$this->alertBack('删除成功！');
    	}else{
            $this->alertBack('删除失败，您删除的用户可能存在销售人员，销售人员不能直接删除！');
        }
        $this->tpl_path('adminlist');
    }
	



}
?>
